﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Security
{
    /// <summary>
    /// 
    /// </summary>
    public static class CryptoHelper
    {
        #region MD5

        /// <summary>
        /// MD5消息摘要算法
        /// </summary>
        /// <param name="encryptString"> 要加密的文本 </param>
        /// <returns></returns>
        public static string Md5Encrypt(this string encryptString)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(encryptString ?? string.Empty, "MD5");
        }

        /// <summary>
        /// SHA1安全哈希算法(适用于数字签名标准里面定义的数字签名算法)
        /// </summary>
        /// <param name="encryptString"></param>
        /// <returns></returns>
        public static string SHA1Encrypt(this string encryptString)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(encryptString ?? string.Empty, "sha1");
        }

        #endregion

        #region DES(一种对称加密算法)

        /// <summary>
        /// DES(一种对称加密算法)加密
        /// </summary>
        /// <param name="encryptString"> 要加密的文本 </param>
        /// <param name="key"> 密钥(8个字符) </param>
        /// <returns> 返回中密后的文本 </returns>
        public static string DesEncrypt(this string encryptString, string key)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(encryptString);
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream,
                    new System.Security.Cryptography.DESCryptoServiceProvider().CreateEncryptor(
                    Encoding.UTF8.GetBytes(key.PadRight(8, '-').Substring(0, 8)),
                    Encoding.UTF8.GetBytes(key.PadRight(8, '-').Substring(0, 8))
                    ),
                    System.Security.Cryptography.CryptoStreamMode.Write
                    )
                    )
                {
                    cryptoStream.Write(buffer, 0, buffer.Length);
                    cryptoStream.FlushFinalBlock();
                }
                return Convert.ToBase64String(memoryStream.ToArray());
            }
        }

        /// <summary>
        /// DES(一种对称加密算法)解密
        /// </summary>
        /// <param name="decryptString"> 要解密的文本 </param>
        /// <param name="key"> 密钥(8个字符) </param>
        /// <returns> 返回解密后的文本 </returns>
        public static string DesDecrypt(this string decryptString, string key)
        {
            byte[] buffer = Convert.FromBase64String(decryptString);
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream,
                    new System.Security.Cryptography.DESCryptoServiceProvider().CreateDecryptor(
                    Encoding.UTF8.GetBytes(key.PadRight(8, '-').Substring(0, 8)),
                    Encoding.UTF8.GetBytes(key.PadRight(8, '-').Substring(0, 8))
                ),
                System.Security.Cryptography.CryptoStreamMode.Write
                    )
                    )
                {
                    cryptoStream.Write(buffer, 0, buffer.Length);
                    cryptoStream.FlushFinalBlock();
                }
                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
        }

        #endregion
    }
}
